import numpy as np
import matplotlib.pyplot as plt

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

x = np.random.randn(1000, 100)
node_num = 100                      # 各隐藏层节点（神经元）数
hidden_layer_size = 5               # 隐藏层数
activations = {}                    # 保存激活值结果


for i in range(hidden_layer_size):
    if i is not 0:
        x = activations[i - 1]

    # w = np.random.randn(node_num, node_num)                       # 梯度消失
    # w = np.random.randn(node_num, node_num) * 0.01                # 表现力受限
    w = np.random.randn(node_num, node_num) / np.sqrt(node_num)     # Xavier 初始值

    z = np.dot(x, w)
    activations[i] = sigmoid(z)

 # 绘制各层激活值分布直方图
for i, a in activations.items():
    plt.subplot(1, len(activations), i + 1)
    plt.title(str(i + 1) + "-layer")
    plt.hist(a.flatten(), 30, range = (0, 1))
plt.show()
